package routes

import (
	"encoding/json"
	"log"
	"net/http"
	"web/database"
)

func registerCompaniesRoutes() {
	http.HandleFunc("/api/companies", handleCompanies)
	http.HandleFunc("/api/getCompany", handleGetCompany)
	http.HandleFunc("/api/insertCompany", handleInsertCompany)
	http.HandleFunc("/api/updateCompany", handleUpdateCompany)
}

func handleCompanies(w http.ResponseWriter, r *http.Request) {
	switch r.Method {
	case http.MethodPost:
		dec := json.NewDecoder(r.Body)
		company := Company{}
		err := dec.Decode(&company)
		if err != nil {
			log.Println(err.Error())
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		enc := json.NewEncoder(w)
		err = enc.Encode(company)
		if err != nil {
			log.Println(err.Error())
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
	default:
		w.WriteHeader(http.StatusMethodNotAllowed)
	}
}

func handleGetCompany(w http.ResponseWriter, r *http.Request) {
	switch r.Method {
	case http.MethodGet:
		var cs []Company
		company := Company{}

		// dec := json.NewDecoder(r.Body)
		// err := dec.Decode(&company)
		// if err != nil {
		// 	log.Println(err.Error())
		// 	w.WriteHeader(http.StatusInternalServerError)
		// 	return
		// }

		sqlStr := "select id, name, address from gocompany where id > ?"
		stmt, _ := database.Db.Prepare(sqlStr)

		rows, _ := stmt.Query(0)

		// 循环读取结果集中的数据
		for rows.Next() {
			rows.Scan(&company.Id, &company.Name, &company.Address)
			cs = append(cs, company)
		}

		enc := json.NewEncoder(w)
		err := enc.Encode(cs)
		if err != nil {
			log.Println(err.Error())
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		defer stmt.Close()
		defer rows.Close()
	default:
		w.WriteHeader(http.StatusMethodNotAllowed)
	}
}

func handleInsertCompany(w http.ResponseWriter, r *http.Request) {
	switch r.Method {
	case http.MethodPost:
		company := Company{}
		dec := json.NewDecoder(r.Body)
		err := dec.Decode(&company)
		if err != nil {
			log.Println(err.Error())
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		sqlStr := "insert into gocompany(name,address) values(?,?)"
		stmt, _ := database.Db.Prepare(sqlStr)

		_, err = stmt.Exec(company.Name, company.Address)
		if err != nil {
			log.Println(err.Error())
			return
		}

		defer stmt.Close()

		// enc := json.NewEncoder(w)
		// err = enc.Encode(company)
		// if err != nil {
		// 	log.Println(err.Error())
		// 	w.WriteHeader(http.StatusInternalServerError)
		// 	return
		// }
	default:
		w.WriteHeader(http.StatusMethodNotAllowed)
	}
}

func handleUpdateCompany(w http.ResponseWriter, r *http.Request) {
	switch r.Method {
	case http.MethodPost:
		company := Company{}
		dec := json.NewDecoder(r.Body)
		err := dec.Decode(&company)
		if err != nil {
			log.Println(err.Error())
			w.WriteHeader(http.StatusInternalServerError)
			return
		}

		sqlStr := "update gocompany set name=?,address=? where id=?"
		stmt, _ := database.Db.Prepare(sqlStr)

		_, err = stmt.Exec(company.Name, company.Address, company.Id)
		if err != nil {
			log.Println(err.Error())
			return
		}

		defer stmt.Close()

		// enc := json.NewEncoder(w)
		// err = enc.Encode(company)
		// if err != nil {
		// 	log.Println(err.Error())
		// 	w.WriteHeader(http.StatusInternalServerError)
		// 	return
		// }
	default:
		w.WriteHeader(http.StatusMethodNotAllowed)
	}
}

// // 预处理查询示例
// func PrepareQueryDemo() (users []Company, err error) {

// 	sqlStr := "select id, name, address from gocompany where id > ?"
// 	stmt, err := database.Db.Prepare(sqlStr)

// 	rows, err := stmt.Query(0)

// 	// 循环读取结果集中的数据
// 	for rows.Next() {
// 		var u = Company{}
// 		err = rows.Scan(&u.Id, &u.Name, &u.Address)
// 		users = append(users, u)
// 	}
// 	defer stmt.Close()
// 	defer rows.Close()

// 	return
// }
